<?php
namespace app\api\controller;
use think\facade\Db;

class Company extends common
{
    private $dbname = 'company';
    public function index()
    {
        return 'company';
    }
    /**
     * 列表分页获取
     * @return string
     */
    public function getSeleList()
    {
        $data = input('get.');

        if(!empty($data['id']) || !empty($data['name'])){
            $list = Db::name($this->dbname)->whereOr('id','=',$data['id'])->whereOr('name','like','%'.$data['name'].'%')->field('id,name,status')->select()->toArray();

        }else{
            $where = [];
            $where[] = ['status','=',1];
            $where[] = ['check','=',1];
            $where[] = ['passed','=',1];
            $list = Db::name($this->dbname)->where($where)->field('id,name,status')->select()->toArray();

        }
        return re_success('获取成功',$list);
    }

    /**
     * 列表分页获取
     * @return string
     */
    public function getlist()
    {
        $data = input('get.');

        $where = [];
        if(!empty($data['name'])){
            $where[] = ['company.name
                |company.username
                |company.license_code
                |company.credit_code
                |company.corporation
                |company.phone
                |company.user_phone
                |company.credit_code
                ','like','%'.$data['name'].'%'];
        }

        if(!empty($data['passed']) ){
            $where[] = ['passed','=',$data['passed']];
        }else{

            $where[] = ['passed','in',[0,1,2]];
        }

        if(!empty($data['check']) ){
            $where[] = ['check','=',$data['check']];
        }else{
            $where[] = ['check','in',[0,1,2]];
        }
        if(!empty($data['status']) ){
            $where[] = ['status','=',$data['status']];
        }else{
            $where[] = ['status','in',[0,1,2]];
        }

        $list = Db::view($this->dbname,'id,logo,address,name,phone,user_phone,username,address,passed,check,status,corporation,credit_code,time')
            ->where($where)
            ->order('status desc,passed desc,id desc')
            ->paginate([
                'list_rows'=> $data['limit'],
                'var_page' => 'page',
            ])->toArray();
        return re_list($list);
    }

    /**
     * 添加
     * @return false|string|void
     */
    public function insert()
    {

        $key = get_rand(4);
        $data = input('post.');
        $data['time']=ThisTime();
        $model = new \app\api\model\Company();
        $data['key'] = $key;
        $data['user_id'] = $this->getThisUser()['uid'];
        $data['license_files'] = $data['license_files']?json_encode($data['license_files']):null;
        $data['corporation_files'] = $data['corporation_files']?json_encode($data['corporation_files']):null;
        $data['credit_files'] = $data['credit_files']?json_encode($data['credit_files']):null;
        $er = $model->add_vali($data);
        if($er){
            return  $er;
        }
        $data['password'] = md5($key.$data['password']);
        //模型数据验证

        //模型写入
        return $model->add($data);
    }

    /**
     * 修改
     * @return false|string|void
     */
    public function update()
    {
        $data = input('put.');
        $data['user_id'] = $this->getThisUser()['uid'];


        $data['license_files'] = $data['license_files']?json_encode($data['license_files']):null;
        $data['corporation_files'] = $data['corporation_files']?json_encode($data['corporation_files']):null;
        $data['credit_files'] = $data['credit_files']?json_encode($data['credit_files']):null;


        //模型验证
        $model = new \app\api\model\Company();
        $er = $model->edit_vali($data);
        if($er){
            return  $er;
        }
        if(isset($data['password'])){
            $key = get_rand(4);
            $data['password'] = md5($key.$data['password']);
            $data['key'] = $key;

        }else{
            unset($data['password']);
            unset($data['key']);
        }
        //移除影响数据的字段
        unset($data['password1']);
        //模型修改
        //注意。。超级管理员只有超级管理员才可以修改，其他账号不可修改。
        if($data['id'] == 1){
          exit('不可修改');
        }
        return $model->edit($data);

    }

    /**
     * 修改
     * @return false|string|void
     */
    public function updateStatus()
    {
        $data = input('put.');
        //模型验证
        $model = new \app\api\model\Company();
        $er = $model->edit_vali($data);
        if($er){
            return  $er;
        }
        return $model->editStatus($data);

    }


    /**查询采用视图查询
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {

        $info = Db::view($this->dbname, '*')->find(input('get.id'));
        if($info){
            $info['license_files'] = $info['license_files']?json_decode($info['license_files']):[];
            $info['corporation_files'] = $info['corporation_files']?json_decode($info['corporation_files']):[];
            $info['credit_files'] = $info['credit_files']?json_decode($info['credit_files']):[];
            unset($info['password']);
            unset($info['key']);
            return re_success('获取成功',$info);
        }else{
            return re_error('获取失败');
        }
    }

    public function token(){
        return re_success('获取成功',input('get.'));

    }
    /**
     * 删除
     * @return false|string|void
     */
    public function dele()
    {
        $data = input('delete.');
        $model = new \app\api\model\Company();
        $er = $model->dele_vali($data);
        if($er){
            return  $er;
        }

        return $model->dele($data['id']);
    }
}
